import 'package:boss/models/commodity.dart';
import 'package:boss/provider/commodity_provider.dart';
import 'package:boss/ui/widget/button/radius_button.dart';
import 'package:boss/ui/widget/custom_tab.dart';
import 'package:boss/ui/widget/form/base_text_form_field.dart';
import 'package:boss/ui/widget/label_button.dart';
import 'package:boss/ui/widget/modal/modal.dart';
import 'package:flutter/material.dart';
import 'package:hi_common/hi_common.dart';
import 'package:provider/provider.dart';

Future<void> showStockModal(BuildContext context, Commodity commodity) async {
  return showModal(context, builder: (context) => _StockModal(commodity));
}

class _StockModal extends StatefulWidget {
  final Commodity commodity;

  const _StockModal(this.commodity);

  @override
  __StockModalState createState() => __StockModalState();
}

class __StockModalState extends State<_StockModal> {
  TextEditingController stockController;
  FocusNode stockFocusNode;

  @override
  void initState() {
    super.initState();

    stockController = TextEditingController(text: widget.commodity.stock == -1 ? '无限' : widget.commodity.stock.toString());
    stockFocusNode = FocusNode();
    stockFocusNode.addListener(() {
      if (stockFocusNode.hasFocus) {
        stockController.selection = TextSelection(baseOffset: 0, extentOffset: stockController.text.length);
      }
    });
  }

  @override
  void dispose() {
    stockController.dispose();
    stockFocusNode.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      width: 600.w,
      height: 610.w,
      padding: EdgeInsets.symmetric(
        horizontal: 60.w,
        vertical: 40.w,
      ),
      child: Column(
        children: <Widget>[
          Expanded(
            child: CustomTab(
              isScrollable: false,
              tabs: ['沽清'],
              tabViews: <Widget>[
                Column(
                  children: <Widget>[
                    SizedBox(
                      height: 20.w,
                    ),
                    BaseTextFormField(
                      controller: stockController,
                      focusNode: stockFocusNode,
                      prefixText: '剩余库存',
                      isRequired: true,
                      textAlign: TextAlign.center,
                      keyboardType: TextInputType.number,
                      maxLength: 5,
                      suffix: Row(
                        children: <Widget>[
                          LabelButton(
                            title: '清零',
                            padding: EdgeInsets.symmetric(
                              horizontal: 14.w,
                            ),
                            onTap: () {
                              stockController.value = TextEditingValue(
                                text: '0',
                                selection: TextSelection.fromPosition(TextPosition(offset: 1)),
                              );
                            },
                          ),
                          LabelButton(
                            title: '无限',
                            padding: EdgeInsets.symmetric(horizontal: 14.w),
                            onTap: () {
                              stockController.value = TextEditingValue(
                                text: '无限',
                                selection: TextSelection.fromPosition(TextPosition(
                                  offset: 2,
                                )),
                              );
                            },
                          )
                        ],
                      ),
                    ),
                  ],
                )
              ],
            ),
          ),
          Container(
            margin: EdgeInsets.only(
              top: 40.w,
            ),
            child: RadiusButton(
              title: '确 认',
              width: 500.w,
              onTap: () async {
                final provider = Provider.of<CommodityProvider>(context, listen: false);
                final res = await provider.updateStock(widget.commodity.id, stockController.text);
                if (res) {
                  Navigator.pop(context);
                }
              },
            ),
          ),
        ],
      ),
    );
  }
}
